home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 October: Technology Seed / ADC Seed CD - October 1999.toast / Carbon SDK 1.0d10c3 / Sample Code / AppearanceSample / RadioGroup.cp < prev    next >
Encoding:
Text File  |  1999-05-01  |  3.3 KB  |  190 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        RadioGroup.cp
  3.  
  4.     Contains:    Class to implement a radio group.
  5.  
  6.     Version:    Appearance 1.0 SDK
  7.  
  8.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                Edward Voas
  13.  
  14.         Other Contact:        7 of 9, Borg Collective
  15.  
  16.         Technology:            OS Technologies Group
  17.  
  18.     Writers:
  19.  
  20.         (edv)    Ed Voas
  21.  
  22.     Change History (most recent first):
  23.  
  24.          <1>     9/11/97    edv        First checked in.
  25. */
  26.  
  27. #include "AppearanceSamplePrefix.h"
  28.  
  29. #include <ControlDefinitions.h>
  30.  
  31. #include "RadioGroup.h"
  32.  
  33. typedef struct
  34. {
  35.     ControlRef        control;
  36.     SInt16            value;
  37. } RadioEntry;
  38.  
  39. RadioGroup::RadioGroup()
  40. {
  41.     fInfo = NewHandle( 0 );
  42.     fValue = 0;
  43.     fMixed = false;
  44. }
  45.  
  46. RadioGroup::~RadioGroup()
  47. {
  48.     DisposeHandle( fInfo );
  49. }
  50.         
  51. void
  52. RadioGroup::AddControl( ControlRef control, SInt16 value )
  53. {
  54.     Size        size;
  55.     SInt16        numItems;
  56.     
  57.     size = GetHandleSize( fInfo );
  58.     numItems = size / sizeof( RadioEntry );
  59.     
  60.     SetHandleSize( fInfo, size + sizeof( RadioEntry ) );
  61.     
  62.     ((RadioEntry*)*fInfo)[ numItems ].value = value;
  63.     ((RadioEntry*)*fInfo)[ numItems ].control = control;
  64.     
  65.     SetValue( value ); 
  66. }
  67.  
  68. SInt16
  69. RadioGroup::GetValue()
  70. {
  71.     return fValue;
  72. }
  73.  
  74. void
  75. RadioGroup::SetValue( SInt16 value )
  76. {
  77.     SInt16        numItems;
  78.     SInt16        i;
  79.     ControlRef    oldControl = nil, newControl = nil;
  80.     
  81.     if ( fMixed )
  82.     {
  83.         numItems = GetHandleSize( fInfo ) / sizeof( RadioEntry );
  84.         
  85.         for ( i = 0; i < numItems; i++ )
  86.         {
  87.             if ( GetControlValue( ((RadioEntry*)*fInfo)[ i ].control ) )
  88.             {
  89.                 SetControlValue( ((RadioEntry*)*fInfo)[ i ].control, 0 );
  90.             }
  91.         }
  92.     }
  93.     
  94.     if ( (value != fValue) || fMixed )
  95.     {
  96.         numItems = GetHandleSize( fInfo ) / sizeof( RadioEntry );
  97.         
  98.         for ( i = 0; i < numItems; i++ )
  99.         {
  100.             if ( ((RadioEntry*)*fInfo)[ i ].value == value )
  101.                 newControl = ((RadioEntry*)*fInfo)[ i ].control;
  102.             
  103.             if ( ((RadioEntry*)*fInfo)[ i ].value == fValue )
  104.                 oldControl = ((RadioEntry*)*fInfo)[ i ].control;
  105.         }
  106.         if ( newControl )
  107.         {
  108.             if ( oldControl )
  109.                 SetControlValue( oldControl, 0 );
  110.         
  111.             SetControlValue( newControl, 1 );
  112.         }
  113.         fValue = value;
  114.     }
  115.     fMixed = false;
  116. }
  117.  
  118. void
  119. RadioGroup::SetMixed( SInt16 value )
  120. {
  121.     SInt16        numItems;
  122.     SInt16        i;
  123.     ControlRef    newControl = nil;
  124.     
  125.     numItems = GetHandleSize( fInfo ) / sizeof( RadioEntry );
  126.     
  127.     for ( i = 0; i < numItems; i++ )
  128.     {
  129.         if ( ((RadioEntry*)*fInfo)[ i ].value == value )
  130.         {
  131.             newControl = ((RadioEntry*)*fInfo)[ i ].control;
  132.             break;
  133.         }
  134.     }
  135.     if ( newControl )
  136.     {
  137.         SetControlValue( newControl, kControlCheckBoxMixedValue );
  138.     }
  139.     fValue = value;
  140.  
  141.     fMixed = true;
  142. }
  143.  
  144. void
  145. RadioGroup::SetValueByControl( ControlRef control )
  146. {
  147.     SInt16        numItems;
  148.     SInt16        i;
  149.     ControlRef    oldControl = nil;
  150.     SInt16        value = 0;
  151.     Boolean        valueFound = false;
  152.         
  153.     if ( fMixed )
  154.     {
  155.         numItems = GetHandleSize( fInfo ) / sizeof( RadioEntry );
  156.         
  157.         for ( i = 0; i < numItems; i++ )
  158.         {
  159.             if ( GetControlValue( ((RadioEntry*)*fInfo)[ i ].control ) )
  160.             {
  161.                 SetControlValue( ((RadioEntry*)*fInfo)[ i ].control, 0 );
  162.             }
  163.         }
  164.     }
  165.     
  166.     numItems = GetHandleSize( fInfo ) / sizeof( RadioEntry );
  167.     
  168.     for ( i = 0; i < numItems; i++ )
  169.     {
  170.         if ( ((RadioEntry*)*fInfo)[ i ].control == control )
  171.         {
  172.             value = ((RadioEntry*)*fInfo)[ i ].value;
  173.             valueFound = true;
  174.         }
  175.         
  176.         if ( ((RadioEntry*)*fInfo)[ i ].value == fValue )
  177.             oldControl = ((RadioEntry*)*fInfo)[ i ].control;
  178.     }
  179.     if ( valueFound )
  180.     {
  181.         if ( oldControl )
  182.             SetControlValue( oldControl, 0 );
  183.     
  184.         SetControlValue( control, 1 );
  185.     
  186.         fValue = value;
  187.     }
  188.     fMixed = false;
  189. }
  190.